package org.edu.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;

import org.edu.model.TeacherLesson;
import org.edu.service.TeacherLessonService;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.SelectableDataModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
@SessionScoped
public class TeacherLessonController extends ListDataModel<TeacherLesson> implements Serializable, SelectableDataModel<TeacherLesson> {

	private static final long serialVersionUID = 1L;
	@Autowired
	private transient TeacherLessonService teacherLessonService;
	private TeacherLesson teacherLesson;
	private List<TeacherLesson> teacherLessons;
	private List<TeacherLesson> filterTeacherLesson;
	private List<TeacherLesson> selectedTeacherLesson;
	private List<SelectItem> selectedLesson = new ArrayList<SelectItem>();
	private Map<Long, Boolean> checked = new HashMap<Long, Boolean>();

	public TeacherLessonController() {
		teacherLesson = new TeacherLesson();
	}

	@PostConstruct
	public void init() {
		teacherLessons = teacherLessonService.teacherLessons();
		selectedLesson = teacherLessonService.selectItems();
	}
	
	public void preparedAddingTeacherLesson() {
		teacherLesson = new TeacherLesson();
	}

	public String save() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			teacherLessonService.save(teacherLesson);
			facesContext.addMessage(null, new FacesMessage("Save Teacher Lesson is Succesful", teacherLesson.getTeacher().getName()+ " "+ teacherLesson.getLesson().getLesson_name()));
			init();
			teacherLesson = new TeacherLesson();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedEditingTeacherLesson(Long teacher_lesson_id) throws Exception{
		teacherLesson = teacherLessonService.findById(teacher_lesson_id);
	}
	
	public String update() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			teacherLessonService.update(teacherLesson);
			facesContext.addMessage(null, new FacesMessage("Update Teacher Lesson is Succesful"));
			init();
			teacherLesson = new TeacherLesson();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedDeletingTeacherLesson(Long teacher_lesson_id) throws Exception{
		teacherLesson = teacherLessonService.findById(teacher_lesson_id);
	}
	
	public String remove() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		try {
			teacherLessonService.remove(teacherLesson);
			facesContext.addMessage(null, new FacesMessage("Delete Teacher Lesson is Succesful", teacherLesson.getTeacher().getName()+ " "+ teacherLesson.getLesson().getLesson_name()));
			init();
			teacherLesson = new TeacherLesson();
		} catch (Exception e) {
			facesContext.addMessage(null, new FacesMessage("Not Succesful"));
			e.printStackTrace();
		}
		return null;
	}
	
	public void preparedAddingLesson(Long teacher_lesson_id) throws Exception{
		teacherLesson = teacherLessonService.findById(teacher_lesson_id);
	}
	
	public void Submit(){
		List<TeacherLesson> checkedItems = new ArrayList<TeacherLesson>();
        for (TeacherLesson teacherLesson : teacherLessons) {
            if (checked.get(teacherLesson.getTeacher_lesson_id())) {
                checkedItems.add(teacherLesson);
            }
        }
        checked.clear();
	}
	
	@Override
	public Object getRowKey(TeacherLesson object) {
		 return teacherLesson.getTeacher_lesson_id();
	}
	
	@Override
	public TeacherLesson getRowData(String rowKey) {
		for(TeacherLesson teacherLes : teacherLessons){
			if(teacherLes.getTeacher_lesson_id().equals(rowKey))  
                return teacherLes;  
		}
		return null;
	}
	
	public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Lesson Selected", ((TeacherLesson) event.getObject()).getLesson().getLesson_code());;  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
 }

	public TeacherLesson getTeacherLesson() {
		if (teacherLesson == null) {
			teacherLesson = new TeacherLesson();
		}
		return teacherLesson;
	}

	public void setTeacherLesson(TeacherLesson teacherLesson) {
		this.teacherLesson = teacherLesson;
	}

	public List<TeacherLesson> getTeacherLessons() {
		return teacherLessons;
	}

	public List<TeacherLesson> getFilterTeacherLesson() {
		return filterTeacherLesson;
	}

	public void setFilterTeacherLesson(List<TeacherLesson> filterTeacherLesson) {
		this.filterTeacherLesson = filterTeacherLesson;
	}

	public Map<Long, Boolean> getChecked() {
		return checked;
	}

	public void setChecked(Map<Long, Boolean> checked) {
		this.checked = checked;
	}

	public List<TeacherLesson> getSelectedTeacherLesson() {
		return selectedTeacherLesson;
	}

	public void setSelectedTeacherLesson(List<TeacherLesson> selectedTeacherLesson) {
		this.selectedTeacherLesson = selectedTeacherLesson;
	}

	public List<SelectItem> getSelectedLesson() {
		return selectedLesson;
	}
}
